package org.hiucung.desktoptranslationtools.utils



object LanguageUtil {

    private val languages: MutableList<LanguageConfig> = mutableListOf()
    private val _exportLanguages: MutableList<LanguageConfig> = mutableListOf()

    fun getImportLanguageConfigs()= _exportLanguages.toList()

    // 根据语言代码查找对应的 Language
    fun findByLanguageCode(code: String): LanguageConfig? {
        return (languages + _exportLanguages).find { it.languageCode.equals(code, ignoreCase = true) }
    }

    // 根据文件名查找对应的 Language
    fun findByValuesFolderName(folderName: String): LanguageConfig? {
        return (languages + _exportLanguages).find { it.valuesFolderName.equals(folderName, ignoreCase = true) }
    }

    // 根据英文名查找对应的 Language
    fun findByEnglishNameFolderName(folderName: String): LanguageConfig? {
        return (languages + _exportLanguages).find { it.englishName.equals(folderName, ignoreCase = true) }
    }


    fun findByLanguageCode(code: String,configs: List<LanguageConfig>): LanguageConfig? {
        return configs.find { it.languageCode.equals(code, ignoreCase = true) }
    }

    // 根据文件名查找对应的 Language
    fun findByValuesFolderName(folderName: String,configs: List<LanguageConfig>): LanguageConfig? {
        return configs.find { it.valuesFolderName.equals(folderName, ignoreCase = true) }
    }

    // 根据英文名查找对应的 Language
    fun findByEnglishNameFolderName(folderName: String,configs: List<LanguageConfig>): LanguageConfig? {
        return configs.find { it.englishName.equals(folderName, ignoreCase = true) }
    }

    private fun initDefaultLanguage(languagesJson: List<LanguageConfig>) {
        languages.clear()
        languages.addAll(languagesJson.filterNotNull())
    }

    fun addExportLanguages(languagesJson: List<LanguageConfig>) {
        val temp = mutableListOf<LanguageConfig>().apply {
            addAll(_exportLanguages)
        }
        temp.addAll(languagesJson.filterNotNull().distinct())
        _exportLanguages.addAll(temp.distinct())
    }




    fun getDefaultLanguage(): List<LanguageConfig> {
        return languages.toList()
    }
}

data class LanguageConfig(
    val language: String="",
    val englishName: String="",
    val languageCode: String="",
    val valuesFolderName: String=""
)